<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>图书管理系统登录</title>
  <link rel="stylesheet" href="../../layui/css/layui.css">
  <link rel="stylesheet" href="../../layui/css/style.css">
</head>
<body>

<div class="login-main">
  <header class="layui-elip">图书管理系统登录</header>
  <form class="layui-form">
    <div class="layui-input-inline">
      <input type="text" name="name" required lay-verify="required" placeholder="学号" autocomplete="off"
             class="layui-input">
    </div>
    <div class="layui-input-inline">
      <input type="password" name="password" required lay-verify="required" placeholder="密码" autocomplete="off"
             class="layui-input">
    </div>
    <div class="layui-input-inline">
      <select name="access" id="access" lay-verify="required">
        <option value="">请选择权限</option>
        <option value="0">管理员</option>
        <option value="1">读者</option>
      </select>
    </div>

    <div class="layui-input-inline login-btn">
      <button lay-submit="" lay-filter="action" class="layui-btn layui-btn-normal">登录</button>
    </div>
    <hr/>
    <p><a href="${pageContext.request.contextPath}/toRegister" class="fl">立即注册</a><a href="javascript:" onclick="forgetPWD()" class="fr">忘记密码？</a></p>
  </form>
</div>
<script type="text/javascript" src="layui/layui.js"></script>
<script type="text/javascript">
	layui.use(['layer', 'form'], function () {
		let layer = layui.layer;
		let form = layui.form;
		let $ = layui.jquery;
		form.on('submit(action)', function (data) {
			$.ajax({
				method: 'POST',
				url: '${pageContext.request.contextPath}/realLogin',
				data: data.field,
        dataType: 'json',
				success: function (res) {
					if (res.status === '0' && res.success) {
						location.href ="${pageContext.request.contextPath}/toMain";
					} else if (res.status === '1' && res.success){
						location.href = "${pageContext.request.contextPath}/toReaderMain";
          } else {
						layer.msg(res.message);
          }
				},
        error: function (res){
					console.log(res);
					layer.msg("登录失败，请检查！");
        }
			})
			return false;
		})
	})

  function forgetPWD(){
		layer.msg("请与管理员联系！")
  }
</script>
</body>
</html>
